﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Services;

namespace WebDataServer
{
    /// <summary>
    /// STXImageWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    [System.Web.Script.Services.ScriptService]
    public class STXImageWebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string UploadImage(string base64Data,string fileName)
        {
            // 保存图片到服务器
            string path = Server.MapPath("~/Images/");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string filename = Path.Combine(path, Guid.NewGuid().ToString() +Path.GetFileName( fileName)); // 或者根据需要选择合适的文件名和扩展名
            byte[] binaryData = Convert.FromBase64String(base64Data);
            Task.Factory.StartNew(() =>
            {
                File.WriteAllBytes(filename, binaryData);
            });
            
            return "Image uploaded successfully.";
        }



    }
}
